package com.bigdata.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bigdata.common.utils.StringUtils;
import com.bigdata.common.core.page.TableDataInfo;
import com.bigdata.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.bigdata.service.domain.bo.FriendLinksBo;
import com.bigdata.service.domain.vo.FriendLinksVo;
import com.bigdata.service.domain.FriendLinks;
import com.bigdata.service.mapper.FriendLinksMapper;
import com.bigdata.service.service.IFriendLinksService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 友情链接Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-25
 */
@RequiredArgsConstructor
@Service
public class FriendLinksServiceImpl implements IFriendLinksService {

    private final FriendLinksMapper baseMapper;

    /**
     * 查询友情链接
     */
    @Override
    public FriendLinksVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询友情链接列表
     */
    @Override
    public TableDataInfo<FriendLinksVo> queryPageList(FriendLinksBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<FriendLinks> lqw = buildQueryWrapper(bo);
        Page<FriendLinksVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询友情链接列表
     */
    @Override
    public List<FriendLinksVo> queryList(FriendLinksBo bo) {
        LambdaQueryWrapper<FriendLinks> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FriendLinks> buildQueryWrapper(FriendLinksBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FriendLinks> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), FriendLinks::getName, bo.getName());
        lqw.eq(bo.getIsVisible() != null, FriendLinks::getIsVisible, bo.getIsVisible());
        lqw.orderByDesc(FriendLinks::getCreateTime);
        return lqw;
    }

    /**
     * 新增友情链接
     */
    @Override
    public Boolean insertByBo(FriendLinksBo bo) {
        FriendLinks add = BeanUtil.toBean(bo, FriendLinks.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改友情链接
     */
    @Override
    public Boolean updateByBo(FriendLinksBo bo) {
        FriendLinks update = BeanUtil.toBean(bo, FriendLinks.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FriendLinks entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除友情链接
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
